//
//  LotteryHeadTitleView.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/24.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "LotteryHeadTitleView.h"
@interface LotteryHeadTitleView()
@property(nonatomic,strong)NSMutableArray *btnArr;
@property(nonatomic,strong)UIView *btnView;
@end
@implementation LotteryHeadTitleView{
    QXLable     *labTitle;
    UIView  *redLine;
    QXLable     *labAdvertising;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.mas_equalTo(@5);
        }];
        
        redLine=[[UIView alloc]init];
        redLine.backgroundColor=NAVCOLOR;
        [self addSubview:redLine];
        [redLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).with.offset(2);
            make.left.equalTo(self).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(2, 10));
        }];
        
        labTitle=[[QXLable alloc]initTitle];
        labTitle.textColor= NAVCOLOR;
        
        [self addSubview:labTitle];
        [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(redLine.mas_right).with.offset(5);
            make.centerY.equalTo(redLine);
        }];
      
        
       
        
        self.btnView = [[UIView alloc] init];
        self.btnView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.btnView];
        [self.btnView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-5);
            make.top.equalTo(line.mas_bottom);
            make.bottom.equalTo(self);
            make.left.equalTo(labTitle.mas_right);
        }];
        self.btnArr = [NSMutableArray array];
        
        
        for (int i = 2; i>=0 ; i--) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i+1001;
            
            [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
            [btn setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            btn.hidden = YES;
            [self.btnView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(labTitle);
            }];
            
            [self.btnArr addObject:btn];
            
        }
        
        [self.btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:8 leadSpacing:0 tailSpacing:0];
        
        UIView *line1=[[UIView alloc]init];
        line1.backgroundColor=DEFAULTLINECOLOR;
        [self addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(@.7);
        }];
    }
    return self;
}

-(void)setIsHiddenBtnView:(BOOL)isHiddenBtnView{
    
    if (isHiddenBtnView) {
        self.btnView.hidden = YES;
    }
}

-(void)buttonClick:(UIButton *)btn{
    
    
    
    //按钮点击事件
    AdvertisingModel *model=self.adModelArr[btn.tag-1001];
    
    if ([model.Title containsString:@"】"]) {
        
        UIPasteboard *copy = [UIPasteboard generalPasteboard];
        NSRange startRange = [model.Title rangeOfString:@"【"];
        NSRange endRange = [model.Title rangeOfString:@"】"];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        NSString *result = [model.Title substringWithRange:range];
        
        [copy setString:result];
        
        [SVProgressHUD showSuccessWithStatus:@"复制成功"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
            
        });
    }else{
        if (model.TransferUrl.length<1) {
            return;
        }
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];
    }
    
    
    
    
    
}
- (void)setTitle:(NSString *)title{
    if (_isHiddenLine) {
        redLine.hidden=YES;
        [labTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).with.offset(10);
        }];
    }
    labTitle.text=title;
}




-(void)setAdModelArr:(NSMutableArray *)adModelArr{
    
    _adModelArr = adModelArr;
    for (int i = 0; i< adModelArr.count; i++) {
        
        AdvertisingModel *model = adModelArr[i];
        UIButton *btn = [self.btnView viewWithTag:1001+i];
        btn.hidden = NO;
        if ([model.Title containsString:@"【"]) {
            
            [btn setAttributedTitle: [NSAttributedString tg_imageTextWithTitle:model.Title] forState:UIControlStateNormal];
            
        } else {
            
            [btn setTitle:model.Title forState:UIControlStateNormal];
            
        }
        
    }
    
    
}

@end
